//
//  WMEHotListViewController.m
//  WMEMusic
//
//  Created by 恩叔 on 16/5/4.
//  Copyright © 2016年 enshu. All rights reserved.
//

#import "WMEHotListViewController.h"
#import "WMESongListCell.h"
//#import "WMEHotListModel.h"
#import "WMEPlayerViewController.h"
#import "WMEPlayerModel.h"
#import "NetwrokHandler.h"
@interface WMEHotListViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)UIImageView *pic;
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *dataArr;
@end

@implementation WMEHotListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createView];
    [self data];
}
- (void)createView
{
    WS(ws);
    self.pic = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 150*HEIGHTSCALE)];
    _pic.backgroundColor = [UIColor redColor];
    [_pic sd_setImageWithURL:[NSURL URLWithString:_model.pic_s444]];
    self.tableView = [[UITableView alloc] init];
    [self.view addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(ws.view.mas_left).with.offset(0);
        make.right.equalTo(ws.view.mas_right).with.offset(0);
        make.top.equalTo(ws.view.mas_top).with.offset(0);
        make.bottom.equalTo(ws.view.mas_bottom).with.offset(0);
    }];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableHeaderView = _pic;
    [_tableView registerClass:[WMESongListCell class] forCellReuseIdentifier:@"hotCell"];
}
- (void)data
{
    NSString *str = [@"http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.billboard.billList&type=1&format=json&offset=0&size=100&from=ios&fields=title,song_id,author,resource_type,havehigh,is_new,has_mv_mobile,album_title,ting_uid,album_id,charge,all_rate,mv_provider&version=5.5.6&from=ios&channel=appstore&operator=1" stringByReplacingOccurrencesOfString:@"type=1" withString:[NSString stringWithFormat:@"type=%@", _model.type]];
    [WMEAFNetworking GETNetWithUrl:str body:nil headerFile:nil response:WMEJOSN success:^(id result) {
        NSArray *arr = result[@"song_list"];
        self.dataArr = [WMESongListModel modelHandlerWithArray:arr];
        [_tableView reloadData];
    } failure:^(NSError *error) {
        NSLog(@"网络解析错误hot");
    }];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_dataArr.count) {
        WMESongListCell *hotCell = [_tableView dequeueReusableCellWithIdentifier:@"hotCell" forIndexPath:indexPath];
        WMESongListModel *model = _dataArr[indexPath.row];
        hotCell.model = model;
        return hotCell;
    }
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_dataArr.count) {
        WMESongListModel *model = _dataArr[indexPath.row];
        NSString *str = [@"http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.song.getInfos&ts=1461321078&songid=7330118&nw=2&l2p=218.9&lpb=320000&ext=MP3&format=json&from=ios&usup=1&lebo=0&aac=0&ucf=4&vid=&res=1&e=qfJshuUl8FS63N7L4gV2HAP%2FtWo%2F43nPR3pY%2FvY8iAo%3D&version=5.5.6&from=ios&channel=appstore&operator=1" stringByReplacingOccurrencesOfString:@"songid=7330118" withString:[NSString stringWithFormat:@"songid=%@", model.song_id]];
        [WMEAFNetworking GETNetWithUrl:str body:nil headerFile:nil response:WMEJOSN success:^(id result) {
            WMEPlayerViewController *playerVC = [[WMEPlayerViewController alloc] init];
            NSMutableArray *arr = [NSMutableArray array];
            arr = [WMEPlayerModel modelHandlerWithArray:result[@"songurl"][@"url"]];
            playerVC.listArr = arr;
            playerVC.dic = result;
            playerVC.songArr = _dataArr;
            playerVC.index = indexPath.row;
            NSString *strURL = result[@"songinfo"][@"lrclink"];
            [NetwrokHandler GETWithURL:strURL completion:^(id result) {
                NSLog(@"result");
                playerVC.str = result;
                [self presentViewController:playerVC animated:YES completion:^{
                    NSLog(@"模态成功");
                }];
            }];
        } failure:^(NSError *error) {
            NSLog(@"网络请求错误");
        }];
    }

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
